package com.mofang.powerdekorhelper.persenter;

import com.mofang.powerdekorhelper.base.BasePresent;
import com.mofang.powerdekorhelper.model.OrderDetial;
import com.mofang.powerdekorhelper.model.ResultMessage;
import com.mofang.powerdekorhelper.utils.Constants;
import com.mofang.powerdekorhelper.utils.L;
import com.mofang.powerdekorhelper.utils.UrlTools;
import com.mofang.powerdekorhelper.utils.http.InitRetrofit;
import com.mofang.powerdekorhelper.utils.http.RetrofitSerives;
import com.mofang.powerdekorhelper.view.OrderDetialView;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by luofei on 2017/8/23 0023.
 */

public class OrderDetialPersenter extends BasePresent<OrderDetialView> {

    private final String TAG = this.getClass().getSimpleName();

    RetrofitSerives retrofitSerives;
    Call<OrderDetial> orderDetialCall;
    Call<ResultMessage> addOrderCall;

    public void getOrderDetial(Integer orderId) {

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("orderId", orderId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String jsonParams = jsonObject.toString();

        L.i(TAG, "jsonParams---------------->" + jsonParams);

        view.showProgress();

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);

        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);

        orderDetialCall = retrofitSerives.getOrderDetail(body);

        orderDetialCall.enqueue(new Callback<OrderDetial>() {
            @Override
            public void onResponse(Call<OrderDetial> call, Response<OrderDetial> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {
                        if (response.body().getResult() != null) {
                            view.setOrderDetial(response.body());
                        } else {
                            view.showNullLayout();
                        }
                    } else {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<OrderDetial> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });


    }

    @Override
    public void stopRequest() {
        if (orderDetialCall != null) {
            orderDetialCall.cancel();
        }
    }

    /**
     * 订单详情
     *
     * @param jsonParams
     */
    public void getCheckOrder(String jsonParams) {

        view.showProgress();
        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);
        orderDetialCall = retrofitSerives.getCheckOrder(body);
        orderDetialCall.enqueue(new Callback<OrderDetial>() {
            @Override
            public void onResponse(Call<OrderDetial> call, Response<OrderDetial> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {
                        if (response.body().getResult() != null) {
                            view.setCheckOrder(response.body());
                        } else {
                            view.showNullLayout();
                        }
                    } else if (response.body().getCode().equals("2")) {
                        view.onError(Constants.ORDER_CODE_EXIST);
                    } else {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<OrderDetial> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    /**
     * 创建订单
     *
     * @param jsonParams
     */
    public void getCreateOrder(String jsonParams) {

        L.i("OrderDetialPersenter", "jsonParams----------->" + jsonParams);

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);
        addOrderCall = retrofitSerives.getAddOrder(body);
        addOrderCall.enqueue(new Callback<ResultMessage>() {
            @Override
            public void onResponse(Call<ResultMessage> call, Response<ResultMessage> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {
                        view.setCommitOrder();
                    } else {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ResultMessage> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }

    /**
     * 修改订单
     *
     * @param jsonParams
     */
    public void getUpdateOrder(String jsonParams) {

        retrofitSerives = InitRetrofit.getInstance().getRetrofit(UrlTools.Services);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), jsonParams);
        addOrderCall = retrofitSerives.getEditOrder(body);
        addOrderCall.enqueue(new Callback<ResultMessage>() {
            @Override
            public void onResponse(Call<ResultMessage> call, Response<ResultMessage> response) {
                if (response.isSuccessful()) {
                    if (response.body().getCode().equals("0")) {
                        view.setUpdateOrder(response.body());
                    } else {
                        view.onError(Constants.REQUEST_ERROR_INFO);
                    }
                } else {
                    view.onError(Constants.REQUEST_ERROR_INFO);
                }
            }

            @Override
            public void onFailure(Call<ResultMessage> call, Throwable t) {
                view.onError(Constants.REQUEST_ERROR_INFO);
            }
        });
    }
}
